package ogameplayer.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.spi.LoggingEvent;

public class TextUI2 extends JFrame {

	private static Logger logger = Logger.getLogger("op.gui");
	private JPanel contentPane;
	private JTextArea textArea;
	private JTextField textField;
	private JButton button;
	private List<String> comands = Collections.synchronizedList(new ArrayList<String>());
	private int cmdIndex = 0;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TextUI2 frame = new TextUI2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TextUI2() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if ( e.getKeyCode() == KeyEvent.VK_UP ) {
					cmdIndex--;
					if (cmdIndex < 0 )
						cmdIndex = comands.size();
				} else if ( e.getKeyCode() == KeyEvent.VK_DOWN ) {
					cmdIndex++;
					if ( cmdIndex > comands.size() )
						cmdIndex = 0;
				} else if ( e.getKeyCode() == KeyEvent.VK_ENTER ) {
					button.doClick(0);
				} else {
					return;
				}
				
				if ( cmdIndex == comands.size() )
					textField.setText("");
				else
					textField.setText(comands.get(cmdIndex));
				
			}
		});
		
		
		button = new JButton("Execute");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = textField.getText();
				if ( ! text.trim().equals("") ) {
					performOperation(text);
					comands.add(text);
				}
				textField.setText("");
				cmdIndex = comands.size();
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
					.addComponent(textField, GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(5)
							.addComponent(button))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(textField, GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)))
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		textArea = new JTextArea();
		panel_1.add(textArea);
		
		TextAreaAppender taa = new TextAreaAppender();
		taa.setTextArea(textArea);
		Logger.getRootLogger().addAppender(taa);
	}

	private void performOperation(String cmd){
		logger.debug("Command " +cmd);
	}
	
	
	public static class TextAreaAppender extends WriterAppender {
		
		private JTextArea jTextArea = null;
		
		public TextAreaAppender() {
			// TODO Auto-generated constructor stub
		}
		
		/** Set the target JTextArea for the logging information to appear. */
		public void setTextArea(JTextArea jTextArea) {
			this.jTextArea = jTextArea;
		}
		/**
		 * Format and then append the loggingEvent to the stored
		 * JTextArea.
		 */
		public void append(LoggingEvent loggingEvent) {
			final String message = loggingEvent.getMessage().toString();

			// Append formatted message to textarea using the Swing Thread.
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					jTextArea.append(message+"\n");
				}
			});
		}
	}

}
